import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'hash',
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/pages'),
      children: [
        {
          path: 'system/centers',
          name: 'centers',
          components: {
            centers: () => import('@/pages/system/centers')
          }
        },
        {
          path: 'system/negotiators',
          name: 'negotiators',
          components: {
            negotiators: () => import('@/pages/system/negotiators')
          }
        },
        {
          path: 'system/departments',
          name: 'departments',
          components: {
            departments: () => import('@/pages/system/departments')
          }
        },
        {
          path: 'system/salesmans',
          name: 'salesmans',
          components: {
            salesmans: () => import('@/pages/system/salesmans')
          }
        },
        {
          path: 'system/promotion_channel',
          name: 'promotion_channel',
          components: {
            promotion_channel: () => import('@/pages/system/promotion_channel')
          }
        },
        {
          path: 'system/reasons_cancellation',
          name: 'reasons_cancellation',
          components: {
            reasons_cancellation: () => import('@/pages/system/reasons_cancellation')
          }
        },
        {
          path: 'system/reasons_not_signing',
          name: 'reasons_not_signing',
          components: {
            reasons_not_signing: () => import('@/pages/system/reasons_not_signing')
          }
        },
        {
          path: 'system/reasons_not_pass',
          name: 'reasons_not_pass',
          components: {
            reasons_not_pass: () => import('@/pages/system/reasons_not_pass')
          }
        },
        {
          path: 'system/warrant_man',
          name: 'warrant_man',
          components: {
            warrant_man: () => import('@/pages/system/warrant_man')
          }
        },
        {
          path: 'system/distribution',
          name: 'distribution',
          components: {
            distribution: () => import('@/pages/system/distribution')
          }
        },
        {
          path: 'system/trial_channel',
          name: 'trial_channel',
          components: {
            trial_channel: () => import('@/pages/system/trial_channel')
          }
        },
        {
          path: 'package',
          name: 'package',
          components: {
            package: () => import('@/pages/package')
          }
        },
        {
          path: 'business',
          name: 'business',
          components: {
            business: () => import('@/pages/business')
          }
        },
        {
          path: 'plan',
          name: 'plan',
          components: {
            plan: () => import('@/pages/plan')
          }
        },
        {
          path: 'warrant',
          name: 'warrant',
          components: {
            warrant: () => import('@/pages/warrant')
          }
        },
        {
          path: 'car_resources',
          name: 'car_resources',
          components: {
            car_resources: () => import('@/pages/car_resources')
          }
        },
        {
          path: 'finance',
          name: 'finance',
          components: {
            finance: () => import('@/pages/finance')
          }
        },
        {
          path: 'statistics/business',
          name: 'Stats_business',
          components: {
            Stats_business: () => import('@/pages/statistics/business')
          }
        },
        {
          path: 'statistics/promotion',
          name: 'Stats_promotion',
          components: {
            Stats_promotion: () => import('@/pages/statistics/promotion')
          }
        },
        {
          path: 'statistics/warrant',
          name: 'Stats_warrant',
          components: {
            Stats_warrant: () => import('@/pages/statistics/warrant')
          }
        },
        {
          path: 'statistics/resource',
          name: 'Stats_resource',
          components: {
            Stats_resource: () => import('@/pages/statistics/resource')
          }
        },
        {
          path: 'statistics/finance',
          name: 'Stats_finance',
          components: {
            Stats_finance: () => import('@/pages/statistics/finance')
          }
        },
        {
          path: 'users/users',
          name: 'users',
          components: {
            users: () => import('@/pages/users/users')
          }
        },
        {
          path: 'users/roles',
          name: 'roles',
          components: {
            roles: () => import('@/pages/users/roles')
          }
        },
        {
          path: 'archives',
          name: 'archives',
          components: {
            archives: () => import('@/pages/archives')
          }
        },
      ]
    },
    {
      path: '/screen',
      name: 'screen',
      component: () => import('@/pages/screen')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/pages/login')
    },
  ]
})

router.beforeEach(({ name }, from, next) => {
  if (name === 'login') {
    next()
  } else {
    const token = sessionStorage.getItem('Token')
    if (token || name === 'screen') {
      next()
    } else {
      next('/login')
    }
  }
})
export default router